package chapter05

import scala.annotation.tailrec

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/4 9:25 PM
 */
object Test10_Recursion {
  def main(args: Array[String]): Unit = {
    println(fact(5))
    println(fact2(5))
  }

  def fact(n: Int): Int = {
    if (n == 0) return 1
    fact(n - 1) * n
  }

  def fact2(n: Int): Int = {
    @tailrec
    def loop(n:Int, currRes:Int): Int = {
      if (n==0) return currRes
      loop(n-1, currRes * n)
    }
    loop(n, 1)
  }

}
